package com.sskj.wallet.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.util.HideUtil;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.StateTransferBean;
import com.sskj.wallet.presenter.RecordStateTransferPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/07/16
 */
@Route(path = RConfig.WALLET_ACTIVITY_RECORD_STATE_TRANSFER)
public class RecordStateTransferActivity extends BaseActivity<RecordStateTransferPresenter> {


    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;

    private BaseAdapter<StateTransferBean> adapter;

    private int page = 1;
    private int size = 10;

    @Override
    public int getLayoutId() {
        return R.layout.wallet_activity_record_stat_etransfer;
    }

    @Override
    public RecordStateTransferPresenter getPresenter() {
        return new RecordStateTransferPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.wallet_zhuanzhangjilu));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintColor(Color.TRANSPARENT)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 20))
                .setFirstDraw(true)
                .setLastDraw(true));
        adapter = new BaseAdapter<StateTransferBean>(R.layout.wallet_item_record_state, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, StateTransferBean data) {
                holder.setText(R.id.tv_mobile, HideUtil.getPhoneHide(data.getMobile()))
                        .setText(R.id.tv_amount, CommonUtil.dealCoinNum(data.getPrice(), data.getType()))
                        .setText(R.id.tv_coin, data.getType())
                        .setText(R.id.tv_date, TimeFormatUtil.formatE(Long.valueOf(data.getAddtime()) * 1000));
            }
        };
    }

    @Override
    public void initData() {
        mPresenter.getRecord(true, page, size);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getRecord(false, page, size);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getRecord(false, page, size);
        });
    }

    public void stopRefresh() {
        if (smartRefreshView != null) {
            smartRefreshView.finishRefresh();
            smartRefreshView.finishLoadMore();
        }
    }

    public void getRecordOK(List<StateTransferBean> beanList) {
        if (beanList == null) {
            return;
        }
        if (adapter == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(beanList);
        } else {
            adapter.addData(beanList);
        }
    }
}
